﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace TM.ExceptionManager
{
    public class TMExceptionHandler : ITMExceptionHandler
    {
        private bool _autoWriteLog;
        private bool _throwable;

        public TMExceptionHandler()
        {
            _autoWriteLog = true;
            _throwable = false;
        }
        public TMExceptionHandler(bool autoWriteLog, bool throwable, string logFile)
        {
            AutoWriteLog = autoWriteLog;
            Throwable = throwable;
            LogFile = logFile;
        }

        #region Implementation of ITMExceptionHandler

        public void Handle(Exception exception)
        {
            WriteLogException(exception);
            ThrowException(exception);
        }
        
        public void Handle(Exception exception, string source)
        {
            WriteLogException(exception, source);
            ThrowException(exception, source);
        }

        public void ClearLog()
        {
            File.Delete(LogFile);
        }

        public bool AutoWriteLog
        {
            get { return _autoWriteLog; }
            set { _autoWriteLog = value; }
        }

        public bool Throwable
        {
            get { return _throwable; }
            set { _throwable = value; }
        }

        public string LogFile { get; set; }

        #endregion

        private void ThrowException(Exception exception, string source = null)
        {
            if(Throwable)
            {
                if(!string.IsNullOrEmpty(source))
                    throw new Exception("Error from: " + source,exception);
                else
                {
                    throw exception;
                }
            }
        }


        private void WriteLogException(Exception exception, string source = null)
        {
            if (AutoWriteLog)
            {
                if(string.IsNullOrEmpty(LogFile)) LogFile = "ExceptionLogs.txt";
                var d = DateTime.Now;
                var strDate = string.Format("\r\n\r\n{0}-{1}-{2} {3}:{4}:{5}::{6}\r\n\r\n", d.Day, d.Month, d.Year, d.Hour, d.Minute,
                                            d.Second, d.Millisecond);
                if (!string.IsNullOrEmpty(source))
                {
                    File.AppendAllText(LogFile,
                            strDate +
                            "Error from: " + source + "\r\n"
                            + exception.Message + "\r\n"
                            + exception.Source + "r\n" +
                            exception.StackTrace
                        );
                }
                else
                {
                    File.AppendAllText(LogFile,
                            strDate +
                            exception.Message + "\r\n"
                            + exception.Source + "r\n" +
                            exception.StackTrace
                        );
                }
            }
        }
    }
}
